<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="分类名称" prop="name">
           <el-input
              v-model="queryParams.name"
              placeholder="请输入分类名称"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
           />
        </el-form-item>
        <el-form-item>
           <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
           <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
     </el-form>

     <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
           <el-button
           color="#1ac575"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['ams:category:add']"
           >新增</el-button>
        </el-col>
        <el-col :span="1.5">
           <el-button 
              type="info"
              plain
              icon="Sort"
              @click="toggleExpandAll"
           >展开/折叠</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
     </el-row>

     <el-table
        v-if="refreshTable"
        v-loading="loading"
        :data="categoryList"
        row-key="id"
        :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
     >
        <el-table-column prop="name" label="分类名称" :show-overflow-tooltip="true" />
        <el-table-column prop="icon" label="图标" align="center" >
           <template #default="scope">
              <svg-icon :icon-class="scope.row.icon" />
           </template>
        </el-table-column>
        <el-table-column prop="sorts" label="排序" />
        
        <el-table-column prop="status" label="状态" >
          <template #default="scope">
              <el-switch @change="handleStatusChange(scope.row)" :active-value="0" :inactive-value="1"
                v-model="scope.row.status">
              </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime">
           <template #default="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
           </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" align="center" width="210" class-name="small-padding fixed-width">
           <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ams:category:edit']">修改</el-button>
              <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['ams:category:add']">新增</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ams:category:remove']">删除</el-button>
           </template>
        </el-table-column>
     </el-table>

     <!-- 添加或修改分类对话框 -->
     <el-dialog :title="title" v-model="open" width="680px" append-to-body>
        <el-form ref="categoryRef" :model="form" :rules="rules" label-width="100px">
           <el-row>
              <el-col :span="24">
                 <el-form-item label="上级分类">
                    <el-tree-select
                       v-model="form.parentId"
                       :data="categoryOptions"
                       :props="{ value: 'id', label: 'name', children: 'children' }"
                       value-key="id"
                       placeholder="选择上级分类"
                       check-strictly
                    />
                 </el-form-item>
              </el-col>
              <el-col :span="24" v-if="form.categoryType != 'F'">
                 <el-form-item label="分类图标" prop="icon">
                    <el-popover
                       placement="bottom-start"
                       :width="540"
                       trigger="click"
                    >
                       <template #reference>
                          <el-input v-model="form.icon" placeholder="点击选择图标" @blur="showSelectIcon" readonly>
                             <template #prefix>
                                <svg-icon
                                   v-if="form.icon"
                                   :icon-class="form.icon"
                                   class="el-input__icon"
                                   style="height: 32px;width: 16px;"
                                />
                                <el-icon v-else style="height: 32px;width: 16px;"><search /></el-icon>
                             </template>
                          </el-input>
                       </template>
                       <icon-select ref="iconSelectRef" @selected="selected" :active-icon="form.icon" />
                    </el-popover>
                 </el-form-item>
              </el-col>
              <el-col :span="12">
                 <el-form-item label="分类名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入分类名称" />
                 </el-form-item>
              </el-col>
              <el-col :span="12">
                 <el-form-item label="显示排序" prop="sorts">
                    <el-input-number v-model="form.sorts" controls-position="right" :min="0" />
                 </el-form-item>
              </el-col>
              <el-col :span="12">
                 <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                       <el-radio label="0">正常</el-radio>
                       <el-radio label="1">停用</el-radio>
                    </el-radio-group>
                 </el-form-item>
              </el-col>

              <el-col :span="24">
                 <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder=""/>
                 </el-form-item>
              </el-col>

           </el-row>
        </el-form>
        <template #footer>
           <div class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
           </div>
        </template>
     </el-dialog>
  </div>
</template>

<script setup name="amsCategory">
import { listCategory,getCategory,addCategory,updateCategory,delCategory,changeCategoryStatus } from "@/api/ams/category";
import SvgIcon from "@/components/SvgIcon";
import IconSelect from "@/components/IconSelect";

const { proxy } = getCurrentInstance();
const { sys_show_hide, sys_normal_disable } = proxy.useDict("sys_show_hide", "sys_normal_disable");

const categoryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const categoryOptions = ref([]);
const isExpandAll = ref(false);
const refreshTable = ref(true);
const iconSelectRef = ref(null);

const data = reactive({
 form: {},
 queryParams: {
   name: undefined,
   visible: undefined
 },
 rules: {
   name: [{ required: true, message: "分类名称不能为空", trigger: "blur" }],
   orderNum: [{ required: true, message: "分类顺序不能为空", trigger: "blur" }],
   path: [{ required: true, message: "路由地址不能为空", trigger: "blur" }]
 },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询分类列表 */
function getList() {
 loading.value = true;
 listCategory(queryParams.value).then(response => {
   categoryList.value = proxy.handleTree(response.data, "id");
   loading.value = false;
 });
}
/** 查询分类下拉树结构 */
function getTreeselect() {
 categoryOptions.value = [];
 listCategory().then(response => {
   const category = { id: 0, name: "主类目", children: [] };
   category.children = proxy.handleTree(response.data, "id");
   categoryOptions.value.push(category);
 });
}
/** 取消按钮 */
function cancel() {
 open.value = false;
 reset();
}
/** 表单重置 */
function reset() {
 form.value = {
   id: undefined,
   parentId: 0,
   name: undefined,
   icon: undefined,
   remark: undefined,
   sorts: 0,
   status: "0"
 };
 proxy.resetForm("categoryRef");
}
/** 展示下拉图标 */
function showSelectIcon() {
 iconSelectRef.value.reset();
}
/** 选择图标 */
function selected(name) {
 form.value.icon = name;
}
/** 搜索按钮操作 */
function handleQuery() {
 getList();
}
/** 重置按钮操作 */
function resetQuery() {
 proxy.resetForm("queryRef");
 handleQuery();
}
/** 新增按钮操作 */
function handleAdd(row) {
 reset();
 getTreeselect();
 if (row != null && row.id) {
   form.value.parentId = row.id;
 } else {
   form.value.parentId = 0;
 }
 open.value = true;
 title.value = "添加分类";
}
/** 展开/折叠操作 */
function toggleExpandAll() {
 refreshTable.value = false;
 isExpandAll.value = !isExpandAll.value;
 nextTick(() => {
   refreshTable.value = true;
 });
}
/** 修改按钮操作 */
async function handleUpdate(row) {
 reset();
 await getTreeselect();
 getCategory(row.id).then(response => {
   form.value = response.data;
   open.value = true;
   title.value = "修改分类";
 });
}
/** 提交按钮 */
function submitForm() {
 proxy.$refs["categoryRef"].validate(valid => {
   if (valid) {
     if (form.value.id != undefined) {
       updateCategory(form.value).then(response => {
         proxy.$modal.msgSuccess("修改成功");
         open.value = false;
         getList();
       });
     } else {
       addCategory(form.value).then(response => {
         proxy.$modal.msgSuccess("新增成功");
         open.value = false;
         getList();
       });
     }
   }
 });
}
// 修改状态
function handleStatusChange(row) {
  let text = row.status == 0 ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.name + '"分类吗?').then(function () {
    let data = {
      id: row.id,
      status: row.status
    }
    return changeCategoryStatus(data);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status == 0 ? 1 : 0;
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
 proxy.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项?').then(function() {
  let data = {id: row.id}
   return delCategory(data);
 }).then(() => {
   getList();
   proxy.$modal.msgSuccess("删除成功");
 }).catch(() => {});
}

getList();
</script>
